<div style="height:550px;width: 100%" id="sidebar_left">
<div class="m_leftbar">
    <dl>
				<dt>
					<a  href="#">互动交流</a>
				</dt>
    </dl>
    <ul>
        <?php
        if($_SESSION['type']=='')
        {
            fs_showmsg('请先登录!','?');
        }
        else
        {
            if($_SESSION['type']=='3')
            { 
            ?>
        <li><a href="?do=message">发邮件</a></li>
        <li><a href="?do=inbox">已发邮件</a></li>
         <li><a href="?do=groupmail&c=already">班级邮件</a></li>
        <li><a href="?do=draft">草稿箱</a></li>
        <?php
            }
            elseif($_SESSION['type']=='2')
            {?>
        <li><a href="?do=message">发邮件</a></li>
        <li><a href="?do=groupmail">已发邮件</a></li>
        <li><a href="?do=message&c=already">收件箱</a></li>
        <li><a href="?do=inbox&c=already">已回复信息</a></li>
        <?php
            }
            else{
                fs_showmsg('您既不是学生也不是老师,不能使用该功能!','?');
            }
        }
        ?>
       
    </ul>
</div>
<?PHP 
if($_SESSION['type']=='3')
{?>
    <script type="text/javascript">
 function getOptionMAJOR(){
  $.getJSON("main/message/option.php",{'bval':$("#departid").val()},function(data){
    $("#majorid").empty();//每次选择前，清空小类的值。
    $("#teacher").empty();
    $("#teacher").html("<option value='-1'>请选择老师</option>");
    $("#majorid").html("<option value='-1'>请选择专业</option>");
    $.each(data, function(index,array){
     var option="<option value='"+array['id']+"'>"+array['name']+"</option>"; 
     $("#majorid").append(option);
    });                
   }); 
 }
  function getOptionTeacher(){
  $.getJSON("main/message/option.php",{'majorid':$("#majorid").val()},function(data){
    $("#teacher").empty();//每次选择前，清空小类的值。
    $("#teacher").html("<option value='-1'>请选择老师</option>");
    $.each(data, function(index,array){
     var option="<option value='"+array['id']+"'>"+array['name']+"</option>"; 
     $("#teacher").append(option);
    });                
   }); 
 }
 $(document).ready(function(){
    $("#departid").change(function(){
        getOptionMAJOR();
    });
    $("#majorid").change(function(){
        getOptionTeacher();
    });
 });
</script>
<div class="m_rightbar" >
    <div class="m_title">发邮件</div>
            <div class="m_sendbox">
                <form method="post" action="">
                    <table cellpadding="0" cellspacing="0" id="m_box">
                        <tr><td>院系：</td> 
                            <td>    
                                        <select name="departid" id="departid" style="width:300px;">
                                                
                                    <option>选择老师所在的系</option>
                                    <?php 
                                    $sql = "select * from nettteach_department";
                                    $query  =$db->query($sql);
                                    while($row = $db->fetch_array($query))
                                    {
                                    ?>
                                    <option value="<?php echo $row['id']?>"><?php echo $row['name']?></option>
                                    <?php
                                    }?>
                                        </select></td>
                        </tr>
                        <tr><td>专业：</td> 
                            <td>    <select name="majorid" id="majorid" style="width:300px;">
                                                <option value="-1">请选择专业</option>
                                        </select></td>
                        </tr>
                        <tr><td>老师：</td> 
                            <td>      <select name="teacher" id="teacher" style="width:300px;">
                                                <option value="-1">请选择老师</option>
                                        </select></td>
                        </tr>
                        <tr>
                            <td>主题:</td>
                            <td><input type="text" name="m_title" style="width: 300px;height: 23px;" id="m_title" /></td>
                        </tr>
                        <tr>
                            <td >内容:</td>
                            <td><textarea  id="m_content" name="m_content" rows="10" cols="45"></textarea></td>
                        </tr>
                        <tr>
                            <td><input type="submit" name="sub" id="sub" value="发送" /></td>
                            <td><input type="submit" name="save" id="save" value="保存为草稿"/></td>
                        </tr>
                    </table>
                </form>
            </div>
</div>
</div>
<script type="text/javascript">
                        function choseClass()
                        {
                                var department_id = $("#department").val();
                                $.ajax({
                                    url: '?do=ajax&c=choseclass',
                                    type: 'post',
                                    data: 'departmentid='+department_id,
                                    dataType: 'html',
                                    async:false,
                                    timeout: 5000,
                                    error: function(){
                                        alert('Error loading XML document');
                                    },
                                    success: function(html){
                                        $("#addclass").html(html);
                                    }
                               })
                        }
</script>
<?php
}?>
<?php
if($_POST['sub']||$_POST['save'])
{
    if($_SESSION['sid']=='')
    {
        fs_showmsg('请登录后再发送!');
    }
    $title = $_POST['m_title'];
    $content = $_POST['m_content'];
    if($title==''||$content=='')
    {
        fs_showmsg('主题或内容不能为空');
    }
    if($_SESSION['type']=='3'){
        $to =$_POST['teacher'];
        if($to=='')
        {
            fs_showmsg('请选择老师后再发送!');
        }
    }else{
        $to =$_POST['classname'];
        if($to=='')
        {
            fs_showmsg('请选择班级后再发送');
        }
    }
    
    if($_POST['sub'] && $_SESSION['type']=='3')
    {
        $sql = "insert into nettteach_message  (`m_from`,`m_to`,`object`,`content`,`sendtime`) values (".$_SESSION['sid'].",".$to.",'".$title."','".$content."',".time().")";
        $query = $db->query($sql);
        if($query)
        {
            fs_showmsg('发送成功!');
        } 
    }else if($_POST['sub'] &&$_SESSION['type']=='2'){
        $sql = "insert into nettteach_message  (`m_from`,`to_classid`,`object`,`content`,`sendtime`) values (".$_SESSION['sid'].",".$to.",'".$title."','".$content."',".time().")";
        $query = $db->query($sql);
        if($query)
        {
            fs_showmsg('发送成功!');
        } 
    }
   
    if($_POST['save'])
    {
        $sql = "insert into nettteach_message  (`m_from`,`m_to`,`object`,`content`,`sendtime`,`is_save`) values (".$_SESSION['sid'].",".$to.",'".$title."','".$content."',".time().",1)";
        $query = $db->query($sql);
        if($query)
        {
            fs_showmsg('保存成功,可以到草稿箱查看.');
        } 
        
    }
    
}
?>
<?php
if($_SESSION['type']=='2'&&$_GET['c']=='already')
{?>
<div class="m_rightbar" >
    <div class="m_title">收件箱</div>
            <div class="m_sendbox">
                <form action="" method="post" name="form1" style="margin:0px">
                <table cellspacing="0" cellpadding="0" id="messagelist">
                    <tr class="td_head" align="center">
                        <td style="width: 5%">选中</td>
                        <td>发件人</td>
                        <td>主题</td>
                        <td>发送时间</td>
                        <td>是否回复</td>
                        <td>操作</td>
                    </tr>
                    
                        <?php
                            $sql ="select * from nettteach_message  where m_to=".$_SESSION['sid']." and is_read=0 and is_save=0 and t_delete='1' order by sendtime desc";
                            $query  =$db->query($sql);
                            while($row = $db->fetch_array($query))
                            {?>
                        <tr class="list_list" align="center">
                            <td><input name='id' type="checkbox" value=<?php echo($row['m_id']);?>></td>
                            <td><?php echo fs_get_objinfo("nettteach_member", "id", $row['m_from'], "realname")?></td>
                            <td><?php echo $row['object']?></td>
                            <td><?php echo date('Y-m-d H:i:s',$row['sendtime'])?></td>
                            <?php
                                $r_sql ="select * from nettteach_reply where m_id=".$row['m_id'];
                                $r_query =$db->query($r_sql);
                                $num = mysql_num_rows($r_query);
                                if($num==0){
                                    $p="未回复";
                                }
                                else
                                {
                                    $p="已回复";
                                }
                            ?>
                            <td><?php  echo $p; ?></td>
                            <td><a href="?do=viewmessage&id=<?php echo $row['m_id']?>">查看</a></td>
                             </tr>
                                <?php
                            }
                        ?>
                </table>
                </form>    
                <div style="padding-left: 10px;line-height: 27px;">
                    <INPUT onclick=CheckAll('selectAll',this) type=checkbox value=on name=chkall>全选
                    <input type="checkbox" name="invest" value="checkbox" onClick="CheckAll()">反选
                    <input type="checkbox" name="invest" value="checkbox" onClick="CheckAll('deleteAll',0)">全不选&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type=submit name="delete" id="delete" style="font-size:9pt;width:50px;" value="删除" onclick="getCheck()" >
                </div>
            </div>
            </div>
</div>
<script type="text/javascript">
function getCheck(){
     var result=new Array();
     if(!document.form1.id.length){ 
        if(document.form1.id.checked){
             result.push(document.form1.id.value)
             }
    } 
    else{ 
         for(i=0;i<document.form1.id.length;i++){
             if(document.form1.id[i].checked){
             result.push(document.form1.id[i].value)
             }
         }
    }
//     alert(result);
if(confirm("是否删除")){
     if(result ==''){
         alert("请选择要删除的邮件");
         return false;
     }else{
            window.location.href="?do=message&deid="+result;
     }
}
}
</script>

<?php }else if($_SESSION['type']=='2'&&$_GET['c']!='already'){ ?>
<script type="text/javascript">
  function getOptionDEPARTMENT(){
  $.getJSON("main/message/option.php",{'departmentid':$("#dueid").val()},function(data){
    $("#departid").html("<option value='-1'>请选择系</option>");
    $("#majorid").html("<option value=''>请选择专业</option>");
    $("#classname").html("<option value=''>请选择班级</option>");
    $.each(data, function(index,array){
     var option="<option value='"+array['id']+"'>"+array['name']+"</option>"; 
     $("#departid").append(option);
    });                
   }); 
 }
 function getOptionMAJOR(){
  $.getJSON("main/message/option.php",{'bval':$("#departid").val()},function(data){
    $("#majorid").empty();//每次选择前，清空小类的值。
    $("#classname").empty();
    $("#classname").html("<option value='-1'>请选择班级</option>");
    $("#majorid").html("<option value='-1'>请选择专业</option>");
    $.each(data, function(index,array){
     var option="<option value='"+array['id']+"'>"+array['name']+"</option>"; 
     $("#majorid").append(option);
    });                
   }); 
 }
  function getOptionCLASS(){
  $.getJSON("main/message/option.php",{'classid':$("#majorid").val(),'dueid':$("#dueid").val()},function(data){
    $("#classname").empty();//每次选择前，清空小类的值。
    $("#classname").html("<option value='-1'>请选择班级</option>");
    $.each(data, function(index,array){
     var option="<option value='"+array['id']+"'>"+array['name']+"</option>"; 
     $("#classname").append(option);
    });                
   }); 
 }
 $(document).ready(function(){
    $("#dueid").change(function(){
        getOptionDEPARTMENT();
    });
    $("#departid").change(function(){
        getOptionMAJOR();
    });
    $("#majorid").change(function(){
        getOptionCLASS();
    });
 });
</script>
<div class="m_rightbar" >
    <div class="m_title">发邮件</div>
            <div class="m_sendbox">
                <form method="post" action="">
                    <table cellpadding="0" cellspacing="0" id="m_box">
                    <tr>
                        <td>届：</td> 
                        <td>      
                                    <select name="dueid" id="dueid" style="width:300px;">
                                            <option value="-1">请选择届</option>
                                            <?php
                                            $sql="select* from nettteach_due where 1=1";
                                            $result=mysql_query($sql);
                                            //循环列出选项
                                                    while($rows=mysql_fetch_array($result))
                                            {?>
                                                    <option value=<?php echo $rows['id']; ?>><?php echo $rows['name'];?></option>
                                            <?php } ?>
                                    </select>
                        </td>
                    </tr>
                    <tr><td>院系：</td> 
                        <td>    
                                    <select name="departid" id="departid" style="width:300px;">
                                            <option value="-1">请选择系</option>
                                    </select></td>
                    </tr>
                    <tr><td>专业：</td> 
                        <td>    <select name="majorid" id="majorid" style="width:300px;">
                                            <option value="-1">请选择专业</option>
                                    </select></td>
                    </tr>
                    <tr><td>班级：</td> 
                        <td>      <select name="classname" id="classname" style="width:300px;">
                                            <option value="-1">请选择班级</option>
                                    </select></td>
                    </tr>
                    <tr>
                        <td>主题:</td>
                        <td><input type="text" name="m_title" style="width: 300px;height: 23px;" id="m_title" /></td>
                    </tr>
                    <tr>
                        <td >内容:</td>
                        <td><textarea  id="m_content" name="m_content" rows="10" cols="45"></textarea></td>
                    </tr>
                    <tr>
                        <td><input type="submit" name="sub" id="sub" value="发送" /></td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
</div>
<?php } ?>
<?php
if($_SESSION['sid']!='' && $_GET['deid']!='')
{
    $userid = $_SESSION['sid'];
    $deleteid = $_GET['deid'];
    $deid=explode(",", $deleteid);
    if($deid!='')
    {
        foreach ($deid as $deid){
            $query = "select * from nettteach_message where m_to=".$userid." and m_id = ".$deid;
            $result = $db->query($query);
            $row = $db->fetch_array($result);
            if($row['s_delete']!='0')
            {
                    $update_sql  = "update nettteach_message set t_delete='0' where m_to=".$userid." and m_id=".$deid;
                    $db->query($update_sql);
            }else{
                    $update_sql  = "update nettteach_message set t_delete='0' where m_to=".$userid." and m_id=".$deid;
                    $db->query($update_sql);
//                    $update_sql  = "delete from nettteach_message where m_to=".$userid." and m_id=".$deid;//删除收件
//                    $db->query($update_sql);
//                    $delete_sql = "delete from nettteach_reply where r_from =".$userid." and m_id=".$deid;//删除回复
//                    $db->query($delete_sql);  暂时屏蔽 不能删除
            }
        
        }
                fs_showmsg('删除成功');
    }
    else
    {
        fs_showmsg('请选择要删除的邮件!');
    }
    
}
?>
